package nckd.wnqjt.tmc.tda.plugin.form;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

import kd.bos.algo.DataSet;
import kd.bos.entity.report.AbstractReportListDataPlugin;
import kd.bos.entity.report.ReportQueryParam;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.QueryServiceHelper;

/**
 * @author husheng
 * @date 2024-11-04 20:08
 * @description
 */
public class ReceivablebillListDataPlugin extends AbstractReportListDataPlugin {
    @Override
    public DataSet query(ReportQueryParam reportQueryParam, Object o) {
        // 收票登记
        String receivablebillSql = "id,company.name nckd_companyname,bizdate nckd_bizdate,draftbilltype nckd_draftbilltype," +
                "amount nckd_amount,draftbillstatus nckd_draftbillstatus,billno nckd_billno,draftbillexpiredate nckd_draftbillexpiredate," +
                "issuedate nckd_issuedate,draftbillno nckd_draftbillno,payeetype,delivername,deliver,drawername nckd_drawername,drawer";
        QFilter filter = new QFilter("rptype", QCP.equals, "receivebill")
                .and("draftbilltype.number", QCP.in, new String[]{"102", "104", "CDMT-79A2C33B"})
                .and("billstatus", QCP.equals, "C")
                .and("bizdate", QCP.less_equals, this.getCurrentDate())
                .and("draftbillexpiredate", QCP.large_equals, this.getCurrentDate())
                .and("draftbillstatus", QCP.in, new String[]{"registered", "endorsed", "pledged", "collocated", "discounted", "collected"})
                .and("draftbilltranstatus", QCP.equals, "success");
        DataSet cdmReceivablebill = QueryServiceHelper.queryDataSet(this.getClass().getName(), "cdm_receivablebill", receivablebillSql, filter.toArray(), null);

        /**
         * 根据交票人类型获取对应类型的交票人全称
         * 交票人类型
         * 公司	bos_org
         * 供应商  bd_supplier
         * 客户	bd_customer
         * 职员	bos_user
         * 其他	other
         */
        DataSet receivablebill1 = cdmReceivablebill.copy().filter("payeetype = 'bos_org'");
        DataSet receivablebill2 = cdmReceivablebill.copy().filter("payeetype = 'bd_supplier'");
        DataSet receivablebill3 = cdmReceivablebill.copy().filter("payeetype = 'bd_customer'");
        DataSet receivablebill4 = cdmReceivablebill.copy().filter("payeetype = 'bos_user'");
        DataSet receivablebill5 = cdmReceivablebill.copy().filter("payeetype = 'other'");

        DataSet bosOrg = QueryServiceHelper.queryDataSet(this.getClass().getName(), "bos_org", "id,name", null, null);
        DataSet bdSupplier = QueryServiceHelper.queryDataSet(this.getClass().getName(), "bd_supplier", "id,name", null, null);
        DataSet bdCustomer = QueryServiceHelper.queryDataSet(this.getClass().getName(), "bd_customer", "id,name", null, null);
        DataSet bosUser = QueryServiceHelper.queryDataSet(this.getClass().getName(), "bos_user", "id,name", null, null);

        DataSet finish1 = receivablebill1.join(bosOrg).on("deliver", "id")
                .select(new String[]{"id", "nckd_companyname", "nckd_draftbilltype", "nckd_amount", "nckd_bizdate", "nckd_issuedate",
                                "nckd_drawername", "nckd_billno", "nckd_draftbillno", "nckd_draftbillexpiredate", "nckd_draftbillstatus"},
                        new String[]{"name as nckd_delivername"}).finish();
        DataSet finish2 = receivablebill2.join(bdSupplier).on("deliver", "id")
                .select(new String[]{"id", "nckd_companyname", "nckd_draftbilltype", "nckd_amount", "nckd_bizdate", "nckd_issuedate",
                                "nckd_drawername", "nckd_billno", "nckd_draftbillno", "nckd_draftbillexpiredate", "nckd_draftbillstatus"},
                        new String[]{"name as nckd_delivername"}).finish();
        DataSet finish3 = receivablebill3.join(bdCustomer).on("deliver", "id")
                .select(new String[]{"id", "nckd_companyname", "nckd_draftbilltype", "nckd_amount", "nckd_bizdate", "nckd_issuedate",
                                "nckd_drawername", "nckd_billno", "nckd_draftbillno", "nckd_draftbillexpiredate", "nckd_draftbillstatus"},
                        new String[]{"name as nckd_delivername"}).finish();
        DataSet finish4 = receivablebill4.join(bosUser).on("deliver", "id")
                .select(new String[]{"id", "nckd_companyname", "nckd_draftbilltype", "nckd_amount", "nckd_bizdate", "nckd_issuedate",
                                "nckd_drawername", "nckd_billno", "nckd_draftbillno", "nckd_draftbillexpiredate", "nckd_draftbillstatus"},
                        new String[]{"name as nckd_delivername"}).finish();
        DataSet finish5 = receivablebill5.select(new String[]{"id", "nckd_companyname", "nckd_draftbilltype", "nckd_amount", "nckd_bizdate", "nckd_issuedate",
                "nckd_drawername", "nckd_billno", "nckd_draftbillno", "nckd_draftbillexpiredate", "nckd_draftbillstatus", "delivername as nckd_delivername"});

        DataSet dataSet = finish1.union(finish2, finish3, finish4, finish5);

        //筛选出 筛选器传入的ids
        Map<String, Object> customParam = reportQueryParam.getCustomParam();
        List<String> list = (ArrayList<String>) customParam.get("id");
        List<Long> longList = list.stream().map(e -> Long.valueOf(e)).collect(Collectors.toList());
        dataSet = dataSet.filter("id in ids", Collections.singletonMap("ids", longList));
        return dataSet;
    }

    private Date getCurrentDate() {
        Calendar cal = new GregorianCalendar();
//        cal.set(11, 0);
//        cal.set(12, 0);
//        cal.set(13, 0);
//        cal.set(14, 0);
        return cal.getTime();
    }
}
